from twisted.internet.protocol import Factory
from twisted.internet.protocol import Protocol
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor

class Echo(Protocol):
    def __init__(self,factory):
        self.factory = factory
    def connectionMade(self):
        self.factory.numProtocols = self.factory.numProtocols+1
        self.transport.write("Welcome! There are currently %d open connections.\n" % (self.factory.numProtocols,))
    def connectionLost(self,reason):
        self.factory.numProtocols = self.factory.numProtocols - 1
    def dataReceived(self,data):
        self.transport.write(data)

class QOTDFactory(Factory):
    def buildProtocal(self, addr):
        return QOTD()

class QOTD(Protocol):
    def connectionMade(self):
        self.transport.write("An apple a day keeps the doctor away\r\n")
        self.transport.loseConnection()

class Answer(LineReceiver):
    answers = {'How Are You?':'Fine', None:'I do not what you mean'}
    def lineReceived(self, line):
        if self.answers.has_key(line):
            self.sendLine(self.answers[line])
        else:
            self.sendLine(self.answers[None])

# 8007 is the port you want to run under, Choose something > 1024
print "sth. print"
endpoint = TCP4ServerEndpoint(reactor, 8007)
endpoint.listen(QOTDFactory())
reactor.run()